﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Data;
using Utils;

public partial class ajax_coupon :  BasePage
{
    // Fields
    private NameValueCollection _system = new NameValueCollection();
    public Maticsoft.BLL.Coupon couponbll = new Maticsoft.BLL.Coupon();
    public Maticsoft.Model.Coupon couponmodel = new Maticsoft.Model.Coupon();
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public Maticsoft.Model.Order ordermodel = new Maticsoft.Model.Order();
    public Maticsoft.BLL.Partner partnerBLL = new Maticsoft.BLL.Partner();
    public Maticsoft.Model.Partner partnermodel = new Maticsoft.Model.Partner();
    public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
    public SystemConfig sysconfig = new SystemConfig();
    public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public Maticsoft.Model.Team teammodel = new Maticsoft.Model.Team();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
    public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();

    // Methods
    protected override void OnLoad(EventArgs e)
    {
        OrderedDictionary dictionary;
        List<string> list;
        base.OnLoad(e);
        this._system = this.sysconfig.GetSystem();
        string str = Helper.GetString(base.Request["action"], string.Empty);
        string id = Helper.GetString(base.Request["id"], string.Empty);
        string pass = Helper.GetString(base.Request["secret"], string.Empty);
        string val = string.Empty;
        string str5 = string.Empty;
        this.sysmodel = this.sysbll.GetModel(1);
        switch (str)
        {
            case "dialog":
                val = WebSiteHelper.LoadPageString("~/ajaxpage/ajax_dialog_coupon.aspx");
                base.Response.Write(WebSiteHelper.GetJson(val, "dialog"));
                break;

            case "query":
                this.couponmodel = this.couponbll.GetModel(id);
                if (this.couponmodel != null)
                {
                    this.partnermodel = this.partnerBLL.GetModel(this.couponmodel.Partner_id);
                    this.teammodel = this.teambll.GetModel(this.couponmodel.Team_id);
                    if (this.teammodel != null)
                    {
                        string str6 = this.teammodel.Expire_time.ToString("yyyy-MM-dd");
                    }
                    if (!this.couponbll.isCoupon(id))
                    {
                        str5 = "#[" + id + "]&nbsp;无效";
                    }
                    else if (this.couponbll.isEnable(id))
                    {
                        if (this.sysbll.GetModel(1) != null)
                        {
                            str5 = "[" + this.sysbll.GetModel(1).Couponname + "]无效";
                        }
                        str5 = string.Concat(new object[] { str5, "<br>消费于：[", this.couponbll.GetModel(id).Consume_time, "]" });
                    }
                    else if (!this.couponbll.isTime(DateTime.Parse(this.couponbll.GetModel(id).Expire_time.ToString())))
                    {
                        str5 = "#[" + id + "]&nbsp;已过期";
                        str5 = string.Concat(new object[] { str5, "<br>过期日期：[", this.couponbll.GetModel(id).Expire_time, "]" });
                    }
                    else
                    {
                        str5 = ("#[" + id + "]&nbsp;有效") + "<br>[" + this.teambll.GetModel(this.couponbll.GetModel(id).Team_id).Title + "]";
                        str5 = string.Concat(new object[] { str5, "<br>有效期至&nbsp;[", this.teambll.GetModel(this.couponbll.GetModel(id).Team_id).Expire_time, "]" });
                    }
                }
                else
                {
                    str5 = "#[" + id + "]&nbsp;无效";
                }
                dictionary = new OrderedDictionary();
                dictionary.Add("html", str5);
                dictionary.Add("id", "coupon-dialog-display-id");
                base.Response.Write(WebSiteHelper.GetJson(dictionary, "updater"));
                break;

            case "consume":
                this.couponmodel = this.couponbll.GetModel(id);
                if (this.couponmodel != null)
                {
                    this.partnermodel = this.partnerBLL.GetModel(this.couponmodel.Partner_id);
                    this.teammodel = this.teambll.GetModel(this.couponmodel.Team_id);
                    if (!this.couponbll.isCoupon(id))
                    {
                        str5 = ("#[" + id + "]&nbsp;无效") + "<br>本次消费失败";
                    }
                    else if (!this.couponbll.ispass(pass, id))
                    {
                        if (this.sysbll.GetModel(1) != null)
                        {
                            str5 = (str5 + "<br>[" + this.sysbll.GetModel(1).Couponname + "]编号密码不正确") + "<br>本次消费失败";
                        }
                    }
                    else if (!this.couponbll.isTime(DateTime.Parse(this.couponbll.GetModel(id).Expire_time.ToString())))
                    {
                        str5 = "#[" + id + "]&nbsp;已过期";
                        str5 = string.Concat(new object[] { str5, "<br>过期时间：[", this.couponbll.GetModel(id).Expire_time, "]" });
                        str5 = "<br>本次消费失败";
                    }
                    else if (this.couponbll.isEnable(id))
                    {
                        str5 = str5 + "<br>#[" + id + "]&nbsp;已消费";
                        str5 = string.Concat(new object[] { str5, "<br>消费于：[", this.couponbll.GetModel(id).Consume_time, "]" }) + "<br>本次消费失败";
                    }
                    else
                    {
                        if (!this.couponbll.isEnable(id))
                        {
                            this.couponbll.updatebycid(id, base.Request.UserHostAddress, DateTime.Now, "Y");
                            this.couponmodel = this.couponbll.GetModel(id);
                            if (this.couponmodel.Credit > 0)
                            {
                                Maticsoft.BLL.Flow flow = new Maticsoft.BLL.Flow();
                                Maticsoft.Model.Flow model = new Maticsoft.Model.Flow();
                                model.User_id = this.couponmodel.User_id;
                                model.Detail_id = this.couponmodel.Id;
                                model.Direction = "income";
                                model.Money = this.couponmodel.Credit;
                                model.Action = "coupon";
                                model.Create_time = DateTime.Now;
                                flow.Add(model);
                                this.userbll.Updatemoney(this.couponmodel.User_id, Convert.ToDecimal(this.couponmodel.Credit), this.userbll.GetModel(this.couponmodel.User_id).Money);
                            }
                        }
                        str5 = "【" + id + "】有效";
                        str5 = string.Concat(new object[] { str5, "<br>消费时间：【", this.couponbll.GetModel(id).Consume_time, "】" }) + "本次消费成功";
                        if (((this._system != null) && (this._system["opencoupon"] != null)) && (this._system["opencoupon"] == "1"))
                        {
                            list = new List<string>();
                            if (this.ordermodel != null)
                            {
                                list.Add(base.ASUser.Mobile);
                                base.SendSMS(list, string.Concat(new object[] { "尊敬的", base.ASSystem.abbreviation, "会员，", base.ASUser.Username, "，您的优惠券:", this.couponmodel.Id, "已于", this.couponbll.GetModel(id).Consume_time, "被消费" }));
                            }
                        }
                    }
                    dictionary = new OrderedDictionary();
                    dictionary.Add("html", str5);
                    dictionary.Add("id", "coupon-dialog-display-id");
                    base.Response.Write(WebSiteHelper.GetJson(dictionary, "updater"));
                }
                break;

            default:
                if (str == "sms")
                {
                    string s = "";
                    this.couponmodel = this.couponbll.GetModel(id);
                    if (this.couponmodel != null)
                    {
                        if ((this.couponbll.GetModel(id).Sms >= 5) && (this.userbll.GetModel(this.couponbll.GetModel(id).User_id).Manager == "Y"))
                        {
                            s = WebSiteHelper.GetJson("短信发送优惠券最多5次", "alert");
                            base.Response.Write(s);
                        }
                        else
                        {
                            int seconds = 0;
                            if (this.couponmodel.Sms_time.HasValue)
                            {
                                TimeSpan span;
                                DateTime time = DateTime.Parse(this.couponmodel.Sms_time.ToString());
                                if (base.ASSystem.smsinterval != "")
                                {
                                    span = (TimeSpan)(time.AddSeconds(double.Parse(base.ASSystem.smsinterval.ToString())) - DateTime.Now);
                                    seconds = span.Seconds;
                                }
                                else
                                {
                                    span = (TimeSpan)(time - DateTime.Now);
                                    seconds = span.Seconds;
                                }
                            }
                            if (seconds > 0)
                            {
                                s = WebSiteHelper.GetJson("你好，请在" + seconds + "秒后，再次尝试短信发送优惠券", "alert");
                                base.Response.Write(s);
                            }
                            else if (((this.couponmodel == null) || !this.Page.User.Identity.IsAuthenticated) || ((this.couponmodel.User_id != this.userbll.Getuserid(this.Page.User.Identity.Name)) && (this.userbll.GetModel(this.couponmodel.User_id).Manager == "N")))
                            {
                                s = WebSiteHelper.GetJson("非法下载", "alert");
                                base.Response.Write(s);
                            }
                            else
                            {
                                list = new List<string>();
                                list.Add(this.userbll.GetModel(this.couponmodel.User_id).Mobile);
                                string sitename = "";
                                if (this.sysmodel != null)
                                {
                                    sitename = this.sysmodel.sitename;
                                }
                                s = "您好，你在" + sitename + "获得优惠券：" + this.couponmodel.Id + "密码: " + this.couponmodel.Secret + " 请及时查看";
                                if (base.SendSMS(list, s))
                                {
                                    s = WebSiteHelper.GetJson(s, "alert");
                                    this.couponmodel.Sms++;
                                    this.couponmodel.Sms_time = new DateTime?(DateTime.Now);
                                    this.couponbll.Update(this.couponmodel);
                                    base.Response.Write(s);
                                }
                                else
                                {
                                    s = WebSiteHelper.GetJson("短信发送失败", "alert");
                                    base.Response.Write(s);
                                }
                            }
                        }
                    }
                }
                else
                {
                    base.Response.Write("对不起，没有此优惠券");
                }
                break;
        }
    }

}